#pragma once
#include <SOIL2/SOIL2.h>
#include <iostream>
#include <vector>
#include <io.h>
#include<glew.h>
using namespace std;

 class Util {
public:
	static unsigned int loadTexture(const char* texImgPath) {
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
		unsigned int textureID = SOIL_load_OGL_texture(texImgPath, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
		if (textureID == 0) {
			cerr << "error in loadTexture:" << texImgPath << endl;
		}
		return textureID;
	}

	static vector<string> getFiles(string dir, string postfix, bool remove_postfix=false) {
		vector<string> files;
		intptr_t Handle;
		struct _finddata_t FileInfo;
		if ((Handle = _findfirst(dir.append("\\*").append(postfix).c_str(), &FileInfo)) == -1)
			cerr << "no such dir";
		do {
			if (strcmp(FileInfo.name, ".") != 0 && strcmp(FileInfo.name, "..") != 0) {
				if (remove_postfix) {
					string part = FileInfo.name;
					part = part.substr(0, part.size() - postfix.size());
					files.push_back(part);
				}
				else
					files.push_back(FileInfo.name);
			}
		} while (_findnext(Handle, &FileInfo) == 0);
		_findclose(Handle);

		return files;
	}

	static string find_postfix(string filePath) {
		struct _finddata_t fileInfo;
		intptr_t handle;

		if ((handle = _findfirst(filePath.append(".*").c_str(), &fileInfo)) == -1)
			return "";
		do {
			string candidate = fileInfo.name;
			int split_idx = candidate.size() - 1;
			while (split_idx >= 0 && candidate[split_idx] != '.') {
				--split_idx;
			}
			assert(split_idx > 0);
			string postfix = candidate.substr(split_idx, candidate.size() - 1);
			if (postfix != ".obj")
				return postfix;
		} while (_findnext(handle, &fileInfo) == 0);
		return "";
	}
};
